﻿using assignment6.Entitty;
using assignment6.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace assignment6
{
    public partial class Form2 : Form
    {
        private int tableIndex;
        private int operateType;
        private List<TextBox> textBoxes = new List<TextBox>();
        
        //新增
        public Form2(int tableIndex, DataGridViewColumnCollection columns)
        {
            this.tableIndex = tableIndex;
            operateType = 1;

            InitializeComponent();
            CreateLayout(columns);
        }

        //编辑
        public Form2(int tableIndex, DataGridViewColumnCollection columns, DataGridViewCellCollection cells)
        {
            this.tableIndex = tableIndex;
            operateType = 0;

            InitializeComponent();
            CreateLayout(columns, cells);
        }

        private void CreateLayout(DataGridViewColumnCollection columns, DataGridViewCellCollection cells = null)
        {
            TableLayoutPanel tableLayoutPanel = new TableLayoutPanel()
            {
                ColumnCount = 2,
                RowCount = columns.Count,
                Dock = DockStyle.Fill,
                Padding = new Padding(10, 20, 20, 0),
                Margin = new Padding(20)
            };
            this.Controls.Add(tableLayoutPanel);

            foreach (DataGridViewColumn col in columns)
            {
                Label label = new Label()
                {
                    Text = col.Name,
                    Padding = new Padding(0, 5, 0, 0)
                };
                TextBox textBox = new TextBox()
                {
                    Dock = DockStyle.Fill,
                    ReadOnly = col.Name == "Id"
                };
                textBoxes.Add(textBox);

                if (cells != null)
                {
                    textBox.Text = cells.Cast<DataGridViewCell>().FirstOrDefault(c => c.OwningColumn.Name == col.Name)?.Value?.ToString() ?? string.Empty;
                }

                tableLayoutPanel.Controls.Add(label);
                tableLayoutPanel.Controls.Add(textBox);
            }
        }

        //确定
        private void button1_Click(object sender, EventArgs e)
        {
            switch (tableIndex)
            {
                case 0: // 学校
                    if (operateType == 0)
                    {
                        SqlService.add(createSchoolObject(), null, null, tableIndex);
                    }
                    else
                    {
                        SqlService.update(createSchoolObject(), null, null, tableIndex);
                    }
                    break;
                case 1: // 班级
                    if (operateType == 0)
                    {
                        SqlService.update(null, createClassObject(), null, tableIndex);
                    }
                    else
                    {
                        SqlService.add(null, createClassObject(), null, tableIndex);
                    }
                    break;
                case 2: // 学生
                    if (operateType == 0)
                    {
                        SqlService.update(null, null, createStudentObject(), tableIndex);
                    }
                    else
                    {
                        SqlService.add(null, null, createStudentObject(), tableIndex);
                    }
                    break;
                default:
                    MessageBox.Show("操作失败！");
                    break;
            }
            MessageBox.Show("操作成功！");
            this.Close();
        }

        //新建学校
        private School createSchoolObject()
        {
            return new School()
            {
                Name = textBoxes[0].Text,
                Number = Convert.ToInt32(textBoxes[1].Text),
                Id = String.IsNullOrEmpty(textBoxes[2].Text) ? 0 : Convert.ToInt32(textBoxes[2].Text),
            };
        }

        //新建班级
        private Class createClassObject()
        {
            return new Class()
            {
                Name = textBoxes[0].Text,
                Number = Convert.ToInt32(textBoxes[1].Text),
                School = new School() { Id = Convert.ToInt32(textBoxes[2].Text) },
                Id = String.IsNullOrEmpty(textBoxes[3].Text) ? 0 : Convert.ToInt32(textBoxes[3].Text),
            };
        }

        //新建学生
        private Student createStudentObject()
        {
            return new Student()
            {
                Name = textBoxes[0].Text,
                Sex = Convert.ToChar(textBoxes[1].Text),
                Age = Convert.ToInt32(textBoxes[2].Text),
                Class = new Class() { Id = Convert.ToInt32(textBoxes[3].Text) },
                Id = String.IsNullOrEmpty(textBoxes[4].Text) ? 0 : Convert.ToInt32(textBoxes[4].Text),
            };
        }
    }
}
